# -*- coding: utf-8 -*-
import re

import time
from sqlalchemy import create_engine, Column,String,Integer
from sqlalchemy.dialects.mysql import MEDIUMTEXT
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import pymysql
from bs4 import BeautifulSoup as bs
from dingdian.models import VulSeeBug
from dingdian.init_mysql import session

import sys
reload(sys)
sys.setdefaultencoding('utf-8')


pattern = re.compile(r'\d{4}-\d+')
cve1Pattern =re.compile(r'\d{4}')
cve2Pattern = re.compile(r'\d+$')

rows = session.query(VulSeeBug).count()
for row in range(1,rows+1):

        print row
        try:
          vul = session.query(VulSeeBug).filter(VulSeeBug.id == row).one()
        #print row

          cveId =vul.cve_id
        except:
          print '************************error',row
          continue
        if cveId is not None:
          print "before:",cveId
          try:
            cveId = 'CVE-'+re.search(cve1Pattern, cveId).group()+'-'+re.search(cve2Pattern, cveId).group()
          except:
            continue
          #print cveId

          session.query(VulSeeBug).filter(VulSeeBug.id == row).update({VulSeeBug.cve_id:cveId})
          print "after:", session.query(VulSeeBug).filter(VulSeeBug.id == row).one().cve_id
        #session.add(new_vul)
        session.commit()


